#include "Line.h"
#include "Fysics/System/ResourceManager.h"
#include "Fysics/Renderer/Renderer.h"

namespace Fysics
{
	Line::Line(const std::initializer_list<glm::vec3>& points, const glm::vec4& color, unsigned int width)
		: Object("", "", { 0, 0, 0 }, { 0, 0, 0 }, { 1, 1, 1 }), m_Color(color), m_Width(width)
	{
		VertexBuffer* vb = VertexBuffer::Create(points.begin(), points.size(), sizeof(glm::vec3));
		BufferLayout layout = {
			{ ShaderDataType::Float3, "position" }
		};
		vb->SetLayout(layout);
		m_VertexArray = VertexArray::Create();
		m_VertexArray->AddVertexBuffer(vb);

		int count = points.size();
		std::vector<unsigned int> indices;
		indices.push_back(0);
		for (int i = 1; i < count - 1; i++)
		{
			indices.push_back(i);
			indices.push_back(i);
		}
		indices.push_back(count - 1);
		IndexBuffer* ib = IndexBuffer::Create(indices.data(), (count - 2) * 2 + 2);
		m_VertexArray->SetIndexBuffer(ib);

		m_ShaderProgram = ResourceManager::GetInstance()->GetShaderProgram("Line");
	}

	Line::~Line()
	{
		delete m_VertexArray;
		delete m_ShaderProgram;
	}

	void Line::OnEvent(Event& event)
	{
	}

	void Line::Draw() const
	{
		Renderer::SubmitModelMatrix(GetModelMatrix());
		Renderer::DrawLine(m_ShaderProgram, m_VertexArray, m_Width, m_Color);
	}
}